const {Project} = require('../models/project')
const xss = require('xss')

class ProjectController {
    // 创建项目
    static async create(v) {
        console.log("vv",v)
    // 检测是否存在项目
    const hasProject = await Project.findOne({
        where: {
          name: v.get('body.name'),
          deleted_at: null
        }
      });
     console.log("hasProject",hasProject)
      // 如果存在，抛出存在信息
      if (hasProject) {
        throw new global.errs.Existing('项目已存在');
      }
  
      // 创建文章
      const project = new Project();
  
      project.name = v.get('body.name');
      project.code = v.get('body.code');
      project.type = v.get('body.type');
      project.language = v.get('body.language');
      project.frame = v.get('body.frame');
      project.apikey = v.get('body.apikey');
      project.image = v.get('body.image');
      project.domain = v.get('body.domain');
      project.description = v.get('body.description');
      project.retainNameConfig = v.get('body.retainNameConfig');
      project.retainIdConfig = v.get('body.retainIdConfig');
      project.retainFieldConfig = v.get('body.retainFieldConfig');
      project.moduleConfig = v.get('body.moduleConfig');
      project.tenantId = v.get('body.tenantId');
  
      return project.save();
    }
    // 获取项目列表
    static async list(params = {}) {
    const {name, page = 1}= params;
    const pageSize = 10
       // 筛选方式
       let filter = {
        deleted_at: null
      };
     // 筛选方式：存在搜索关键字
     if (name) {
        filter.name = {
          [Op.like]: `%${xss(name)}%`
        };
      }

      const list = await Project.scope('iv').findAndCountAll({
          limit: pageSize, //每页10条
          offset: (page - 1) * pageSize,
          where: filter,
          order: [
            ['created_at', 'DESC']
          ],
          attributes: {
            exclude: ['updated_at']
          },
      })
      return {
        rows: list.rows,
        // 分页
        pagination: {
            current_page: parseInt(page),
            per_page: 10,
            count: list.count,
            total: list.count,
            total_pages: Math.ceil(list.count / 10),
        }
      }
    }
    // 更新项目
    static async update(id, v) {
        // 查询项目
        const project = await Project.findByPk(id);
        if(project){
            throw new global.errs.NotFound('没有找到相关项目');
        }
        // 更新
        project.name = v.get('body.name');
        project.code = v.get('body.code');
        project.type = v.get('body.type');
        project.language = v.get('body.language');
        project.frame = v.get('body.frame');
        project.apikey = v.get('body.apikey');
        project.image = v.get('body.image');
        project.domain = v.get('body.domain');
        project.description = v.get('body.description');
        project.retainNameConfig = v.get('body.retainNameConfig');
        project.retainIdConfig = v.get('body.retainIdConfig');
        project.retainFieldConfig = v.get('body.retainFieldConfig');
        project.moduleConfig = v.get('body.moduleConfig');
        project.tenantId = v.get('body.tenantId');
        return project.save();
    }
    // 删除项目
    static async destroy(v) {
        // 检测是否存在项目
        const project = await Project.findOne({
            where: {
                id,
                deleted_at: null
              }
        })
         // 不存在抛出错误
        if (!project) {
            throw new global.errs.NotFound('没有找到相关项目');
    
        }
        // 软删除项目
        project.destroy()
    }
    // 项目详情
    static async detail(id) {
        const project = await Project.findOne({
            where: {
                id
            }
        })
           // 不存在抛出错误
        if (!project) {
            throw new global.errs.NotFound('没有找到相关项目');
        }
        return project;
    }

}
module.exports = {
    ProjectController
  }
  